package com.anolesoft.amljmes.fortyone.service.impl;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import com.anolesoft.amljmes.fortyone.dto.*;
import com.anolesoft.amljmes.fortyone.service.mapper.*;
import com.anolesoft.amljmes.fortyone.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.anolesoft.amljmes.fortyone.controller.MesStoveBaseController;
import com.anolesoft.amljmes.fortyone.controller.StoveSaveDTO;
import com.anolesoft.amljmes.fortyone.entity.BasRclMachines;
import com.anolesoft.amljmes.fortyone.entity.MesGjgyxxFksz;
import com.anolesoft.amljmes.fortyone.entity.MesStoveBase;
import com.anolesoft.amljmes.fortyone.entity.MesStoveFabinfo;
import com.anolesoft.amljmes.fortyone.entity.MesStoveQuality;
import com.anolesoft.amljmes.fortyone.entity.MesStoveQualityCustomer;
import com.anolesoft.amljmes.fortyone.service.MesStoveBaseService;
import com.anolesoft.amljmes.fortyone.vo.QualityShowVo.QualityShowVoBuilder;
import com.anolesoft.common.core.domain.LoginUser;
import com.anolesoft.common.core.exception.BaseException;
import com.anolesoft.yr.util.SecurityUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;

@Service
public class MesStoveBaseServiceImpl implements MesStoveBaseService {
	@Autowired
	private MesGjgyxxFkszMapper mesGjgyxxFkszMapper;
	@Autowired
	private BasRclMachinesMapper basRclMachinesMapper;
	@Autowired
	private MesStoveBaseServiceMapper mesStoveBaseServiceMapper;
	@Autowired
	private MesStoveFabinfoServiceMapper mesStoveFabinfoServiceMapper;
	@Autowired
	private StfEmployeeMapper stfEmployeeMapper;
	
	
	@Override
	public void updateMesStoveBaseWj(String objuid) {
		List<MesStoveFabinfoDto> list = mesStoveFabinfoServiceMapper.MesStoveFabinfoAll(objuid);
		boolean fiag = true;
		for (MesStoveFabinfoDto mesStoveFabinfoDto : list) {
			if("0".equals(mesStoveFabinfoDto.getInpectState())) {
				fiag = false;
			}
		}
		if(fiag) {
			mesStoveBaseServiceMapper.updateMesStoveFabinfosStateWj(objuid);
		}
		 
	}
	// 根据id查询
	@Override
	public MesStoveBaseDto queryMesStoveBaseById(String objuid) {
		return mesStoveBaseServiceMapper.queryMesStoveBaseById(objuid);
	}

	// 根据炉次编号查询
	@Override
	public MesStoveBaseDto queryMesStoveBaseByLc(String furnaceid) {
		return mesStoveBaseServiceMapper.queryMesStoveBaseByLc(furnaceid);
	}

	// 根据工艺方法简称查询
	@Override
	public MesStoveBaseDto queryMesStoveBaseByGyffjc(String gyffjc) {
		return mesStoveBaseServiceMapper.queryMesStoveBaseByGyffjc(gyffjc);
	}

	// 根据设备名称查询
	@Override
	public MesStoveBaseDto queryMesStoveBaseBySbmc(String devicename) {
		return mesStoveBaseServiceMapper.queryMesStoveBaseBySbmc(devicename);
	}

	// 根据所在厂房查询
	@Override
	public MesStoveBaseDto queryMesStoveBaseBySzcf(String plant) {
		return mesStoveBaseServiceMapper.queryMesStoveBaseBySzcf(plant);
	}

	// 根据派工时间查询
	@Override
	public MesStoveBaseDto queryMesStoveBaseByPgsj(Date pgsj) {
		return mesStoveBaseServiceMapper.queryMesStoveBaseByPgsj(pgsj);
	}

	// 根据id修改状态、开工时间
	@Transactional
	@Override
	public int updateMesStoveBaseKg(List<String> objuids) {
		int count = 0;
		for (String objuid : objuids) {
			count += mesStoveBaseServiceMapper.updateMesStoveBaseKg(objuid,SecurityUtils.getLoginUser().getUserUid());
		}
		return count;
	}

	@Transactional
	@Override
	public int updateMesStoveBaseWg(List<String> objuids) {
		int count = 0;
		for (String objuid : objuids) {
			count += mesStoveBaseServiceMapper.updateMesStoveBaseWg(objuid);
		}
		return count;
	}

	// 根据id修改状态、派工时间
	@Override
	public int updateMesStoveBasePg(List<String> objuids) {
		int count = 0;
		for (String objuid : objuids) {
			count += mesStoveBaseServiceMapper.updateMesStoveBasePg(objuid);
		}
		return count;
	}

	// 填写
	@Override
	public int updateMesStoveBaseStateTx(MesStoveBaseDto inFoDtoParam) {
		int count = mesStoveBaseServiceMapper.updateMesStoveBaseStateTx(inFoDtoParam);
		return count;
	}

	// 检验
	@Override
	public int updateMesStoveBaseStateJy(MesStoveBaseDto inFoDtoParam) {
		int count = mesStoveBaseServiceMapper.updateMesStoveBaseStateJy(inFoDtoParam);
		return count;
	}

	@Override
	public List<MesStoveBaseDto> queryMesStoveBaseInfo(MesStoveBaseController.DemoVO demoVO) {
		List<MesStoveBaseDto> queryMesStoveBaseDto = mesStoveBaseServiceMapper.queryMesStoveBaseSQL(demoVO);
		return queryMesStoveBaseDto;
	}

	// 设备下拉
	@Override
	public List<BasRclMachinesDto> MachinesnameAll(String factoryId) {
		List<BasRclMachinesDto> MachinesnameAll = mesStoveBaseServiceMapper.MachinesnameAll(factoryId);
		return MachinesnameAll;
	}

	@Transactional
	@Override
	public int deleteMesStoveBase(List<String> objuids) {
		objuids.forEach(objuid -> {
			mesStoveBaseServiceMapper.deleteMesStoveBase(objuid);
			mesStoveFabinfoServiceMapper.deleteByMesStoveBaseUid(objuid);
		});
		return 0;
	}

	@Override
	public int addMesStoveBase(MesStoveBaseDto inFoDtoParam) {
		System.out.println(">>>>>>add" + inFoDtoParam.toString());
//    	String aString = inFoDtoParam.getDevicename();
//    	String deviceid = mesStoveBaseServiceMapper.querySbbh(aString);
//    	inFoDtoParam.setDeviceid(deviceid);
		int count = mesStoveBaseServiceMapper.addMesStoveBase(inFoDtoParam);
		return count;
	}

	// 查询厂房，工段
	@Override
	public BasRclMachinesDto queryBasRclMachines(String machinesname, String factoryId) {
		System.out.println(">>>>>" + machinesname);
		String[] arr = machinesname.split("-");
		String a = arr[1];
		System.out.println("@@@@@@@@@@@????????" + a);
		return mesStoveBaseServiceMapper.queryBasRclMachines(a, factoryId);
	}

	@Override
	public List<MesGjgyxxFksz> getCappSimpleNameOptions(String keyWord) {
		LoginUser loginUser = SecurityUtils.getLoginUser();
		return mesGjgyxxFkszMapper.queryMesGjgyxxFkszForSelect(keyWord, loginUser.getFacCode());
	}

	@Override
	public List<BasRclMachines> getDevicenameOptions(String keyWord) {
		LoginUser loginUser = SecurityUtils.getLoginUser();
		return basRclMachinesMapper.queryBasRclMachinesForSelect(keyWord, loginUser.getFacCode());
	}

	@Transactional
	@Override
	public void saveStove(StoveSaveDTO stoveSaveDTO) {
		LoginUser loginUser = SecurityUtils.getLoginUser();
		if (StrUtil.isEmptyOrUndefined(stoveSaveDTO.getGjgyxxObj())) {
			throw new BaseException("请选择工艺方法简称");
		}
		if (StrUtil.isEmptyOrUndefined(stoveSaveDTO.getDeviceUid())) {
			throw new BaseException("请选择设备");
		}
		MesStoveBase mesStoveBase = BeanUtil.copyProperties(stoveSaveDTO, MesStoveBase.class);
		mesStoveBase.setObjuid(IdUtil.simpleUUID());
		mesStoveBase.setCjr(loginUser.getUserUid());
		mesStoveBase.setCjsj(LocalDateTime.now());
		mesStoveBase.setState(0);
		mesStoveBase.setQuantity(0);
		mesStoveBase.setFactoryId(loginUser.getFacCode());

		mesStoveBaseServiceMapper.save(mesStoveBase);
	}

	@Override
	public Page<StoveVo> getStoveList(StoveQueryParamDto stoveQueryParamDto) {
		LoginUser loginUser = SecurityUtils.getLoginUser();
		stoveQueryParamDto.getCondition().setFactoryId(loginUser.getFacCode());
		Page<StoveVo> page = new Page<>(stoveQueryParamDto.getPageNo(), stoveQueryParamDto.getPageSize());
		List<StoveVo> stoveVos = mesStoveBaseServiceMapper.queryStoveVosByCondition(stoveQueryParamDto.getCondition(),
				page);
		page.setRecords(stoveVos);
		return page;
	}

	@Override
	public MesOrderTraceInfoDto checkStoveDispatchScanIn(String scanIn, String mesStoveBaseUid, String gxmgjzf) {
		LoginUser loginUser = SecurityUtils.getLoginUser();
		String factoryId = "41";
		
		MesOrderTraceInfoDto mesOrderTraceInfoDto = mesStoveBaseServiceMapper.queryMesOrderTraceInfoDto(scanIn,
				factoryId);

		if (mesOrderTraceInfoDto == null) {
			throw new BaseException("条码错误或制造记录不存在");
		}
		mesOrderTraceInfoDto.setMesStoveBaseUid(mesStoveBaseUid);
		String processname = mesOrderTraceInfoDto.getProcessname();
		if (StrUtil.isEmpty(processname)) {
			throw new BaseException("此制造记录当前工序名是空，无法继续操作");
		}
		if (mesOrderTraceInfoDto.getPutNum() <= 0) {
			throw new BaseException("当前制造记录可验收数量是0，不可派工");
		}
		// 查找这个炉次下当前所有的制造记录工序信息
		List<MesStoveFabinfo> mesStoveFabinfos = mesStoveBaseServiceMapper.queryMesStoveFabinfos(mesStoveBaseUid);
		// 先判断一下当前扫码的制造记录当前工序信息是否已经在此炉次中，如果已经存在，给予用户提示
		int[] indexOfAll = ListUtil.indexOfAll(mesStoveFabinfos, info -> {
			return info.getMesProcessUid().equals(mesOrderTraceInfoDto.getMesProcessTraceUid());
		});
		if (indexOfAll.length > 0) {
			throw new BaseException("当前您扫码的制造记录当前工序信息已在此炉次完成派工，不能重复扫码！");
		}

		if (!processname.contains(gxmgjzf)) {
			throw new BaseException("当前工序名是: " + processname + ", 当前炉次关键字符是: " + gxmgjzf + ", 不满足派工条件");
		}

		// 查询当前产品制造记录是否已在其他炉次下派工，如果已派工给予用户提示
		List<String> furnaceids = mesStoveBaseServiceMapper
				.queryFurnaceidsByMesProcessTraceUid(mesOrderTraceInfoDto.getMesProcessTraceUid(), mesStoveBaseUid);
		if (!furnaceids.isEmpty()) {
			throw new BaseException("当前制造记录工序信息已经在炉次编号为[" + furnaceids.get(0) + "]派工，不能再次在此炉次派工。");
		}

		String curProcessBiaoji = StrUtil.nullToEmpty(mesOrderTraceInfoDto.getBiaoji());

		// 如果扫码加入的当前制造记录检验标记不是空并且当前炉次下有制造记录，走下面检验标记的校验
		if (!StrUtil.isEmpty(curProcessBiaoji) && !mesStoveFabinfos.isEmpty()) {
			// 提取这个炉次下所有工序的检验标记
			List<String> biaojiInCurStoves = mesStoveFabinfos.stream().map(mesStoveFabinfo -> {
				String biaoji = mesStoveFabinfo.getBiaoji();
				return StrUtil.nullToEmpty(biaoji).toUpperCase().trim();
			}).distinct().filter(biaoji -> {
				return !StrUtil.isEmpty(biaoji);
			}).collect(Collectors.toList());

			// 如果当前炉次下所有的工序检验标记不是空，并且检验标记和当前扫码的检验标记不同，给予用户提示
			if (!biaojiInCurStoves.isEmpty() && !biaojiInCurStoves.contains(curProcessBiaoji)) {
				throw new BaseException(
						"当前炉次内已派工的制造记录工序中包含的检验标记有: " + biaojiInCurStoves.stream().collect(Collectors.joining(","))
								+ ", 当前扫码的这本制造记录检验标记是: " + mesOrderTraceInfoDto.getBiaoji() + ", 不能继续操作");
			}
		}
		return mesOrderTraceInfoDto;
	}

	@Transactional
	@Override
	public void saveMesStoveFabinfo(MesOrderTraceInfoDto mesOrderTraceInfoDto) {
		MesStoveBase mesStoveBase = mesStoveBaseServiceMapper
				.queryMesStoveBaseByObjuid(mesOrderTraceInfoDto.getMesStoveBaseUid());
		LoginUser loginUser = SecurityUtils.getLoginUser();
		MesStoveFabinfo mesStoveFabinfo = new MesStoveFabinfo();
		mesStoveFabinfo.setObjuid(IdUtil.simpleUUID());
		mesStoveFabinfo.setFortyoneCode(mesOrderTraceInfoDto.getFabrecordid());
		mesStoveFabinfo.setOrderCode(mesOrderTraceInfoDto.getOrdercode());
		mesStoveFabinfo.setFortyoneGxid(mesOrderTraceInfoDto.getCurgongxu());
		mesStoveFabinfo.setFortyoneGxname(mesOrderTraceInfoDto.getProcessname());
		mesStoveFabinfo.setState(0);
		mesStoveFabinfo.setPaigonghao(mesOrderTraceInfoDto.getAssignmentid());
		mesStoveFabinfo.setJixing(mesOrderTraceInfoDto.getJx());
		mesStoveFabinfo.setTuhao(mesOrderTraceInfoDto.getPartid());
		mesStoveFabinfo.setYssl(mesOrderTraceInfoDto.getPutNum());
		mesStoveFabinfo.setYcjyhgs(mesOrderTraceInfoDto.getPutNum());
		mesStoveFabinfo.setHgps(0);
		mesStoveFabinfo.setRbps(0);
		mesStoveFabinfo.setFps(0);
		mesStoveFabinfo.setCjr(loginUser.getEmpUid());
		mesStoveFabinfo.setFactoryId(loginUser.getFacCode());
		mesStoveFabinfo.setCjsj(LocalDateTime.now());
		mesStoveFabinfo.setStoyeFabinfoObj(mesOrderTraceInfoDto.getMesStoveBaseUid());
		mesStoveFabinfo.setBasRclMachinesUid(mesStoveBase.getDeviceUid());
		mesStoveFabinfo.setBiaoji(mesOrderTraceInfoDto.getBiaoji());
		mesStoveFabinfo.setMesProcessUid(mesOrderTraceInfoDto.getMesProcessTraceUid());

		mesStoveFabinfoServiceMapper.insert(mesStoveFabinfo);
	}

	@Transactional
	@Override
	public void updateDispatchCountAndState(String objuid) {
		MesStoveBase mesStoveBase = mesStoveBaseServiceMapper.queryDispatchCountAndState(objuid);
		LoginUser loginUser = SecurityUtils.getLoginUser();
		Integer state = null;
		// 如果当前炉次状态是已生成炉次，炉次内的制造记录数量大于0，将状态改成已派工
		if (mesStoveBase.getState() == 0 && mesStoveBase.getQuantity() > 0) {
			state = 1;
			mesStoveBase.setPgr(loginUser.getEmpUid());
			mesStoveBase.setPgsj(LocalDateTime.now());
		}
		// 如果当前炉次状态是已派工，炉次内的制造记录数量是0，将状态改成已生成炉次
		if (mesStoveBase.getState() == 1 && mesStoveBase.getQuantity() == 0) {
			state = 0;
		}
		mesStoveBase.setState(state);
		mesStoveBaseServiceMapper.updateStateAndCount(mesStoveBase);
	}

	@Transactional
	@Override
	public void updateProcessKg(List<String> objuids) {
		LoginUser loginUser = SecurityUtils.getLoginUser();
		String userCode = loginUser.getUsername();
		String userUid = loginUser.getEmpUid();
		objuids.forEach(objuid -> {
			
			String gxmgjzf = StrUtil.nullToEmpty(mesStoveBaseServiceMapper.getProcessNameByObjuid(objuid));
			if (StrUtil.isEmptyOrUndefined(gxmgjzf)) {
				throw new BaseException("请填写工序名关键字符");
			}
			QualityShowVo qualityShowVo = createQualityShowVoByProcessName(objuid, gxmgjzf, false);
			if(qualityShowVo.getNeedCheck()) {
				//需要反馈工人信息
				mesStoveFabinfoServiceMapper.updateProcessWorkerInfo(userCode, userUid, objuid);
			}else {
				//不需要反馈工人信息
				mesStoveFabinfoServiceMapper.updateProcessWorkerInfo(null, null, objuid);
			}
			mesStoveFabinfoServiceMapper.updateCzys(objuid);
		});
	}

	@Override
	public QualityFormVo getQualityFormInfo(String objuid) {
		MesStoveQuality mesStoveQuality = mesStoveFabinfoServiceMapper
				.queryMesStoveFabinfoServiceMapperByMesStoveBaseUid(objuid);
		MesStoveQualityVo mesStoveQualityVo = BeanUtil.copyProperties(mesStoveQuality, MesStoveQualityVo.class);
		EmpDto empDto = mesStoveBaseServiceMapper.queryEmpDtoByObjuid(objuid);
		if (empDto != null) {
			mesStoveQualityVo.setEmpName(empDto.getEmpName());
			mesStoveQualityVo.setChecker(empDto.getCheckerName());
		}

		List<MesStoveQualityCustomer> mesStoveQualityCustomers = mesStoveFabinfoServiceMapper
				.queryMesStoveQualityCustomersByMesStoveBaseUid(objuid);
		// 查询炉次的工序名称关键字符
		String gxmgjzf = StrUtil.nullToEmpty(mesStoveBaseServiceMapper.getProcessNameByObjuid(objuid));
		if (StrUtil.isEmptyOrUndefined(gxmgjzf)) {
			throw new BaseException("请填写工序名关键字符");
		}

		QualityShowVo qualityShowVo = createQualityShowVoByProcessName(objuid, gxmgjzf, false);
		QualityFormVo vo = new QualityFormVo();
		vo.setMesStoveQualityVo(mesStoveQualityVo);
		vo.setMesStoveQualityCustomers(mesStoveQualityCustomers);
		vo.setQualityShowVo(qualityShowVo);
		return vo;
	}

	/**
	 * 根据炉次 工序名称字符判断质量表单内显示的字段 和 需要反馈信息的角色
	 * 
	 * @param mesStoveBaseUid
	 * @param isThrowNoMatchException
	 * @return
	 */
	private QualityShowVo createQualityShowVoByProcessName(String mesStoveBaseUid, String gxmgjzf,
			boolean isThrowNoMatchException) {
		QualityShowVoBuilder builder = QualityShowVo.builder();

		// 需要检验扫码 默认 false
		//现在它的含义是是否需要操作者扫码
		boolean needCheck = true;
		// 查询当前炉次下的
		List<MesStoveFabinfo> mesStoveFabinfos = mesStoveBaseServiceMapper.queryMesStoveFabinfos(mesStoveBaseUid);
		if (mesStoveFabinfos.isEmpty()) {
			throw new BaseException("炉次反馈信息错误");
		}

		// 关键字匹配默认false
		boolean isMatch = false;

		if (gxmgjzf.contains("入厂")) {
			// 平面度 直线度 粗糙度
			builder.pmdShow(QualityProp.create(true)).zxdShow(QualityProp.create(true, "zxd"))
					.ccdShow(QualityProp.create(true));
			isMatch = true;
			needCheck = false;
		}
		if (gxmgjzf.contains("清洗")) {
			// 清洗剂名称 设备编号 清洗温度
			builder.qxjmcShow(QualityProp.create(true)).sbbhShow(QualityProp.create(true))
					.qxwdShow(QualityProp.create(true));
			isMatch = true;
		}
		if (gxmgjzf.contains("干燥")) {
			// 干燥方式 设备编号 干燥温度 干燥时间
			builder.gzfsShow(QualityProp.create(true, "gzfs")).sbbhShow(QualityProp.create(true))
					.gzwdShow(QualityProp.create(true)).gzsjShow(QualityProp.create(true));
			isMatch = true;
		}
		if (gxmgjzf.contains("脆性")) {
			// 脆性等级
			builder.cxdjShow(QualityProp.create(true, "cxdj"));
			isMatch = true;
			needCheck = false;
		}
		if (gxmgjzf.contains("硬度检查")) {
			// 硬度值（HRC） 硬度值（HRA） 硬度值（HR15N） 硬度值（HB） 硬度值（HV）
			builder.ydzHrcShow(QualityProp.create(true, "ydzHrc")).ydzHraShow(QualityProp.create(true))
					.ydzHr15nShow(QualityProp.create(true)).ydzHbShow(QualityProp.create(true))
					.ydzHvShow(QualityProp.create(true));
			isMatch = true;
			needCheck = false;
		}
		if (gxmgjzf.contains("校正")) {
			// 校正前平面度 校正直线度 校正后平面度 校正后直线度
			builder.jzqpmdShow(QualityProp.create(true)).jzhzxdShow(QualityProp.create(true))
					.jzhpmdShow(QualityProp.create(true)).jzhzxdShow(QualityProp.create(true));
			isMatch = true;
		}
		if (gxmgjzf.contains("检验")) {
			// 机械性能理化试验报告编号 金项检查理化试验报告编号 平面度 直线度 表面颜色
			builder.jxxnlhsybgbhShow(QualityProp.create(true)).jxjclhsybgbhShow(QualityProp.create(true))
					.pmdShow(QualityProp.create(true)).zxdShow(QualityProp.create(true))
					.bmysShow(QualityProp.create(true));
			isMatch = true;
			needCheck = false;
		}
		if (gxmgjzf.contains("预热") || gxmgjzf.contains("固溶") || gxmgjzf.contains("冷处理") || gxmgjzf.contains("氧化处理")
				|| gxmgjzf.contains("奥氏体化") || gxmgjzf.contains("时效") || gxmgjzf.contains("沉淀硬化")
				|| gxmgjzf.contains("火")) {
			builder.zkgzylShow(QualityProp.create(true)).sbbhShow(QualityProp.create(true, "sbbh"))
					.dydjrwdShow(QualityProp.create(true, "dydjrwd"))
					.dydjrbwsjShow(QualityProp.create(true, "dydjrbwsj")).sbbhdedjrbwShow(QualityProp.create(true))
					.dedjrwdShow(QualityProp.create(true)).dedjrbwsjShow(QualityProp.create(true))
					.sbbhdsdjrbwShow(QualityProp.create(true)).dsdjrwdShow(QualityProp.create(true))
					.dsdjrbwsjShow(QualityProp.create(true)).sbbhdsidjrbwShow(QualityProp.create(true))
					.dsidjrwdShow(QualityProp.create(true)).dsidjrbwsjShow(QualityProp.create(true))
					.lqfsShow(QualityProp.create(true, "lqfs")).shuiwenShow(QualityProp.create(true))
					.youwenShow(QualityProp.create(true)).lpqtmcShow(QualityProp.create(true))
					.tqyqShow(QualityProp.create(true)).clwdShow(QualityProp.create(true))
					.zysjShow(QualityProp.create(true));
			isMatch = true;
		}
		if (gxmgjzf.contains("氮")) {
			builder.sbbhShow(QualityProp.create(true, "sbbh")).bmhhwdShow(QualityProp.create(true))
					.bmhhsjShow(QualityProp.create(true)).yyhwdShow(QualityProp.create(true))
					.yyhsjShow(QualityProp.create(true)).dydstjrwdShow(QualityProp.create(true, "dydstjrwd"))
					.dydstbwsjShow(QualityProp.create(true, "dydstbwsj"))
					.dydstafjlShow(QualityProp.create(true, "dydstafjl")).dedstjrwdShow(QualityProp.create(true))
					.dedstbwsjShow(QualityProp.create(true)).dedstafjlShow(QualityProp.create(true))
					.dsdstjrwdShow(QualityProp.create(true)).dsdstbwsjShow(QualityProp.create(true))
					.dsdstafjlShow(QualityProp.create(true)).dygsyqcsjShow(QualityProp.create(true, "dygsyqcsj"))
					.dygsyschdShow(QualityProp.create(true, "dygsyschd")).degsyqcsjShow(QualityProp.create(true))
					.degsyschdShow(QualityProp.create(true)).dsgsyqcsjShow(QualityProp.create(true))
					.dsgsyschdShow(QualityProp.create(true)).qljzmcShow(QualityProp.create(true, "qljzmc"))
					.clwdShow(QualityProp.create(true, "clwd"));
			isMatch = true;
		}
		if (gxmgjzf.contains("碳") && !gxmgjzf.contains("氮")) {
			builder.sbbhShow(QualityProp.create(true, "sbbh")).dydstwdShow(QualityProp.create(true, "dydstwd"))
					.dydstsjShow(QualityProp.create(true, "dydstsj")).dydsttsShow(QualityProp.create(true, "dydstts"))
					.dedstwdShow(QualityProp.create(true, "dedstwd")).dedstsjShow(QualityProp.create(true, "dedstsj"))
					.dedsttsShow(QualityProp.create(true, "dedstts"))
					.dygsyqcsjShow(QualityProp.create(true, "dygsyqcsj"))
					.dygsyschdShow(QualityProp.create(true, "dygsyschd")).degsyqcsjShow(QualityProp.create(true))
					.degsyschdShow(QualityProp.create(true)).dsgsyqcsjShow(QualityProp.create(true))
					.dsgsyschdShow(QualityProp.create(true)).lltsShow(QualityProp.create(true, "llts"))
					.clwdShow(QualityProp.create(true, "clwd"));
			isMatch = true;
		}
		// 提取所有标记
		List<String> biaojiInCurStoves = mesStoveFabinfos.stream().map(mesStoveFabinfo -> {
			String biaoji = mesStoveFabinfo.getBiaoji();
			return StrUtil.nullToEmpty(biaoji).toUpperCase().trim();
		}).distinct().filter(biaoji -> {
			return !StrUtil.isEmpty(biaoji);
		}).collect(Collectors.toList());
		if (biaojiInCurStoves.contains("JY3") || biaojiInCurStoves.contains("JY1")
				|| biaojiInCurStoves.contains("JY2")) {
			needCheck = true;
		}
		builder.needCheck(needCheck);
		builder.isMatch(isMatch);

		if (isThrowNoMatchException && !isMatch) {
			throw new BaseException("工序名关键字符[" + gxmgjzf + "]， 不能匹配任何质量表单");
		}

		return builder.build();
	}

	@Transactional(rollbackFor = Exception.class)
	@Override
	public void saveQualityFormInfo(SaveQualityDto saveQualityDto) {
		LoginUser loginUser = SecurityUtils.getLoginUser();
		String objuid = saveQualityDto.getObjuid();

		if(saveQualityDto.getState() == 3) {
			//当前状态是已完工, 此步骤是工人填写质量信息, 填写完成后将状态改成4(已填写质量信息)
			MesStoveBase mesStoveBase = new MesStoveBase();
			mesStoveBase.setObjuid(saveQualityDto.getObjuid());
			mesStoveBase.setTxr(loginUser.getEmpUid());
			mesStoveBaseServiceMapper.updateQualityTxr(mesStoveBase);
			
			mesStoveBase.setObjuid(saveQualityDto.getObjuid());
			mesStoveBase.setState(4);
			mesStoveBaseServiceMapper.updateState(mesStoveBase);
		}
		
		if(saveQualityDto.getState() == 4 && !saveQualityDto.getIsChecker()) {
			//如果当前状态是 已填写质量信息 并且不是检验员点击 说明是工人进行修改质量表单 此时不改变状态
			MesStoveBase mesStoveBase = new MesStoveBase();
			mesStoveBase.setObjuid(saveQualityDto.getObjuid());
			mesStoveBase.setTxr(loginUser.getEmpUid());
			mesStoveBaseServiceMapper.updateQualityTxr(mesStoveBase);
		}

		mesStoveFabinfoServiceMapper.deleteQualityByMesStoveBaseUid(objuid);

		MesStoveQuality quality = saveQualityDto.getQuality();
		quality.setObjuid(IdUtil.simpleUUID());
		quality.setStoyeFabinfoObj(objuid);
		quality.setCjr(loginUser.getEmpUid());
		quality.setCjsj(LocalDateTime.now());
		quality.setFactoryId(loginUser.getFacCode());

		mesStoveFabinfoServiceMapper.insertQuality(quality);

		mesStoveFabinfoServiceMapper.deleteQualityCustomersByMesStoveBaseUid(objuid);
		List<MesStoveQualityCustomer> qualityCustomers = saveQualityDto.getQualityCustomers();
		if (qualityCustomers != null && !qualityCustomers.isEmpty()) {
			for (int i = 0; i < qualityCustomers.size(); i++) {
				MesStoveQualityCustomer mesStoveQualityCustomer = qualityCustomers.get(i);
				mesStoveQualityCustomer.setObjuid(IdUtil.simpleUUID());
				mesStoveQualityCustomer.setSeqNum(i * 10);
				mesStoveQualityCustomer.setCjr(loginUser.getEmpUid());
				mesStoveQualityCustomer.setMesStoveQualityUid(objuid);
				mesStoveQualityCustomer.setMesStoveQualityUid(quality.getObjuid());
				mesStoveQualityCustomer.setCjsj(LocalDateTime.now());
				mesStoveQualityCustomer.setFactoryId(loginUser.getFacCode());
				mesStoveFabinfoServiceMapper.insertMesStoveQualityCustomer(mesStoveQualityCustomer);
			}
		}
	}

	@Transactional(rollbackFor = Exception.class)
	@Override
	public void qualityFormcheckInput(SaveQualityDto saveQualityDto) {
		LoginUser loginUser = SecurityUtils.getLoginUser();
		EmpDto emp = mesStoveBaseServiceMapper.queryEmpDtoByObjuid(loginUser.getEmpUid());
		if(StrUtil.isEmpty(emp.getNowQuarters())) {
			throw new BaseException("此员工信息中没有现岗信息, 不能继续操作");
		}
		if(!emp.getNowQuarters().contains("检验")) {
			throw new BaseException("此员工现岗是: " + emp.getNowQuarters() + ", 不能进行检验操作");
		}
		
//		mesStoveFabinfoServiceMapper.deleteQualityByMesStoveBaseUid(objuid);
//
//		MesStoveQuality quality = saveQualityDto.getQuality();
//		quality.setObjuid(IdUtil.simpleUUID());
//		quality.setStoyeFabinfoObj(objuid);
//		quality.setCjr(loginUser.getEmpUid());
//		quality.setCjsj(LocalDateTime.now());
//		quality.setFactoryId(loginUser.getFacCode());
//
//		mesStoveFabinfoServiceMapper.insertQuality(quality);
//		mesStoveFabinfoServiceMapper.deleteQualityCustomersByMesStoveBaseUid(objuid);
//		List<MesStoveQualityCustomer> qualityCustomers = saveQualityDto.getQualityCustomers();
//		if (qualityCustomers != null && !qualityCustomers.isEmpty()) {
//			for (int i = 0; i < qualityCustomers.size(); i++) {
//				MesStoveQualityCustomer mesStoveQualityCustomer = qualityCustomers.get(i);
//				mesStoveQualityCustomer.setObjuid(IdUtil.simpleUUID());
//				mesStoveQualityCustomer.setSeqNum(i * 10);
//				mesStoveQualityCustomer.setCjr(loginUser.getEmpUid());
//				mesStoveQualityCustomer.setMesStoveQualityUid(objuid);
//				mesStoveQualityCustomer.setMesStoveQualityUid(quality.getObjuid());
//				mesStoveQualityCustomer.setCjsj(LocalDateTime.now());
//				mesStoveQualityCustomer.setFactoryId(loginUser.getFacCode());
//				mesStoveFabinfoServiceMapper.insertMesStoveQualityCustomer(mesStoveQualityCustomer);
//			}
//		}
//
//
//		//状态改成  5：已检验
		MesStoveBase mesStoveBase = new MesStoveBase();
		mesStoveBase.setObjuid(saveQualityDto.getObjuid());
		mesStoveBase.setState(5);
		mesStoveBaseServiceMapper.updateState(mesStoveBase);
		
		mesStoveBase.setJyr(emp.getObjuid());
		mesStoveBase.setJysj(LocalDateTime.now());
		mesStoveBaseServiceMapper.updateJyr(mesStoveBase);
	}

	@Override
	public Page<FortyoneApplyVo> getFortyoneApplyList(FortyoneApplyDto fortyoneApplyDto) {
		Page<FortyoneApplyVo> page = new Page<>(fortyoneApplyDto.getPageNo(), fortyoneApplyDto.getPageSize());
		List<FortyoneApplyVo> fortyoneApplyVos = mesStoveBaseServiceMapper.queryFortyoneApplyVosByCondition(fortyoneApplyDto.getCondition(),
				page);
		for (FortyoneApplyVo vo : fortyoneApplyVos){
			if (!StrUtil.isEmpty(vo.getTxr())){
				vo.setTxr(stfEmployeeMapper.queryNameByObjuid(vo.getTxr()));
			}
		}
		page.setRecords(fortyoneApplyVos);
		return page;
	}

	@Override
	public Page<MesQualityUpdatelogVo> getFortyoneExamineList(FortyoneApplyDto fortyoneApplyDto) {
		Page<MesQualityUpdatelogVo> page = new Page<>(fortyoneApplyDto.getPageNo(), fortyoneApplyDto.getPageSize());
		List<MesQualityUpdatelogVo> list = mesStoveBaseServiceMapper.queryFortyoneExamineVosByCondition(fortyoneApplyDto.getCondition(),
				page);
		for (MesQualityUpdatelogVo vo : list){
			if (!StrUtil.isEmpty(vo.getSqr())){
				vo.setSqr(stfEmployeeMapper.queryNameByObjuid(vo.getSqr()));
			}
			if (!StrUtil.isEmpty(vo.getShr())){
				vo.setShr(stfEmployeeMapper.queryNameByObjuid(vo.getShr()));
			}
			if (!StrUtil.isEmpty(vo.getSpr())){
				vo.setSpr(stfEmployeeMapper.queryNameByObjuid(vo.getSpr()));
			}
			if (!StrUtil.isEmpty(vo.getZdm())){
				vo.setZdm(new ZdmDto().getZdm(vo.getZdm()));
			}
		}
		page.setRecords(list);
		return page;
	}

	@Override
	public Page<MesQualityUpdatelogVo> getFortyoneAdoptList(FortyoneApplyDto fortyoneApplyDto) {
		Page<MesQualityUpdatelogVo> page = new Page<>(fortyoneApplyDto.getPageNo(), fortyoneApplyDto.getPageSize());
		List<MesQualityUpdatelogVo> list = mesStoveBaseServiceMapper.queryFortyoneAdoptVosByCondition(fortyoneApplyDto.getCondition(),
				page);
		for (MesQualityUpdatelogVo vo : list){
			if (!StrUtil.isEmpty(vo.getSqr())){
				vo.setSqr(stfEmployeeMapper.queryNameByObjuid(vo.getSqr()));
			}
			if (!StrUtil.isEmpty(vo.getShr())){
				vo.setShr(stfEmployeeMapper.queryNameByObjuid(vo.getShr()));
			}
			if (!StrUtil.isEmpty(vo.getSpr())){
				vo.setSpr(stfEmployeeMapper.queryNameByObjuid(vo.getSpr()));
			}
			if (!StrUtil.isEmpty(vo.getZdm())){
				vo.setZdm(new ZdmDto().getZdm(vo.getZdm()));
			}
		}
		page.setRecords(list);
		return page;
	}

	@Override
	public Page<MesQualityUpdatelogVo> getFortyoneHistoryList(FortyoneApplyDto fortyoneApplyDto) {
		Page<MesQualityUpdatelogVo> page = new Page<>(fortyoneApplyDto.getPageNo(), fortyoneApplyDto.getPageSize());
		List<MesQualityUpdatelogVo> list = mesStoveBaseServiceMapper.queryFortyoneHistoryVosByCondition(fortyoneApplyDto.getCondition(),
				page);
		for (MesQualityUpdatelogVo vo : list){
			if (!StrUtil.isEmpty(vo.getSqr())){
				vo.setSqr(stfEmployeeMapper.queryNameByObjuid(vo.getSqr()));
			}
			if (!StrUtil.isEmpty(vo.getShr())){
				vo.setShr(stfEmployeeMapper.queryNameByObjuid(vo.getShr()));
			}
			if (!StrUtil.isEmpty(vo.getSpr())){
				vo.setSpr(stfEmployeeMapper.queryNameByObjuid(vo.getSpr()));
			}
			if (!StrUtil.isEmpty(vo.getZdm())){
				vo.setZdm(new ZdmDto().getZdm(vo.getZdm()));
			}
		}
		page.setRecords(list);
		return page;
	}

	@Override
	public List<FortyoneVo> getFortyoneList(String objuid) {
		String stoyeFabinfoObj = mesStoveBaseServiceMapper.getStoyeFabinfoObj(objuid);
		List<FortyoneVo> list = mesStoveBaseServiceMapper.getFortyoneList(stoyeFabinfoObj);
		return list;
	}

	@Override
	public List<FortyoneVo> getFortyoneListNew(String objuid) {
		List<FortyoneVo> list = mesStoveBaseServiceMapper.getFortyoneList(objuid);
		return list;
	}

	@Override
	public String getYz(String objuid, String zljl) {
		String yz = mesStoveBaseServiceMapper.getYz(objuid,zljl);
		return yz;
	}

	@Transactional
	@Override
	public void saveModifyApply(ModifyDto modifyDto) {
		LoginUser loginUser = SecurityUtils.getLoginUser();
		FortyoneApplyVo applyVo = mesStoveBaseServiceMapper.queryByobjuid(modifyDto.getCondition().getObjuid());
		if (applyVo == null) {
			applyVo = mesStoveBaseServiceMapper.queryByobjuid1(modifyDto.getCondition().getObjuid());
		}
		MesQualityUpdatelogVo vo = new MesQualityUpdatelogVo();
		vo.setZlxxzj(modifyDto.getCondition().getObjuid());
		vo.setLcbh(applyVo.getFurnaceid());
		vo.setLczj(applyVo.getStoyeFabinfoObj());
		vo.setGxmgjzf(applyVo.getGxmgjzf());
		vo.setZzjl(applyVo.getFortyoneCode());
		vo.setGxh(applyVo.getFortyoneGxid());
		vo.setGxm(applyVo.getFortyoneGxname());
		vo.setZdm(modifyDto.getCondition().getZljl());
		vo.setGgq(modifyDto.getCondition().getYz());
		vo.setGgh(modifyDto.getCondition().getXgz());
		vo.setSqr(loginUser.getEmpUid());
		vo.setZt(0);
		vo.setCdh(applyVo.getFactoryId());
		if (StrUtil.isEmpty(vo.getLcbh())){
			vo.setLcbh("0");
		}
		if (StrUtil.isEmpty(vo.getZzjl())){
			vo.setZzjl("0");
		}
		mesStoveBaseServiceMapper.saveModifyApply(vo);
	}

	@Transactional
	@Override
	public void go(String objuid) {
		LoginUser loginUser = SecurityUtils.getLoginUser();
		Integer zt = 3;
		mesStoveBaseServiceMapper.updateModifyApply(objuid, loginUser.getEmpUid(), zt);
	}

	@Transactional
	@Override
	public void back(String objuid) {
		LoginUser loginUser = SecurityUtils.getLoginUser();
		Integer zt = 1;
		mesStoveBaseServiceMapper.updateModifyApply(objuid, loginUser.getEmpUid(), zt);
	}

	@Transactional
	@Override
	public void adopt(String objuid) {
		LoginUser loginUser = SecurityUtils.getLoginUser();
		MesQualityUpdatelogVo vo = mesStoveBaseServiceMapper.queryLogVoByObjuid(objuid);
		Integer zt = 2;
		mesStoveBaseServiceMapper.updateModifyApply1(objuid, loginUser.getEmpUid(), zt);
		mesStoveBaseServiceMapper.updateQuality(vo.getZlxxzj(),vo.getZdm(),vo.getGgh());


	}
}
